'''
        demo.py - 
        Used to demonstrate the features of the AlignedSent class

        Tim Kane (385199)
        Baolin Jing (271965)
        Zhe Zhang (304914)
        19 March 2010
'''

import AlignedSent
from collections import defaultdict

def demo(translation):
    '''
    demo(translation) takes an dictionary of AlignedSent objects.
    For each use case specified in file "use_cases",
    demo calls relevant methods in AlignedSent class and produces the use case result to stdout.    

        @type  translation: dict
        @param translation: an dictionary of AlignedSent objects
    '''

    print
    casefile = open("use_cases",'r')
    # Iterate through every use case in the file
    use_case_functions=[use_case_1, use_case_2, use_case_3, use_case_4, use_case_5];
    for usecase in casefile:
        # Skip over comment lines
        if usecase[0] == '#' or usecase[0] == '\n':
            continue
        args = usecase.split()
        case_type = int(args[0])
        args = args[1:]
        
        use_case_functions[case_type-1](translation, args);
        
    casefile.close()

def use_case_1(translation,args):
    # Parsing line, get arguments of a use case
    index           = int(args[0])
    source_lang     = args[1]
    source_position = int(args[2])

    if source_lang == "English":
        target_lang = "French"
    else:
        target_lang = "English"
    
    # Retrieve AlignedSent Object from the dictionary
    aligned_sentences = translation[index]
    
    # Print Use Case Demo
    print "\nIn sentence no. %s, find the %s word(s) " \
          "aligned with the %sth word in the %s sentence" % (
                index, 
                target_lang, 
                source_position, 
                source_lang)

    # Retrieve the Alignment Entries
    alignments = aligned_sentences.nth_lookup (AlignedSent.SOURCELANG,
                                               source_position)
    
    if alignments == []:
        # Print for null mappings
        print 'Word No.%s in the %s sentence "%s" is not aligned with ' \
              'any words in the %s sentence' % (
                   source_position, 
                   source_lang,
                   aligned_sentences.get_word (AlignedSent.SOURCELANG, 
                                               source_position),
                   target_lang)
    else:
        # Print for non-empty mappings
        print 'Word No.%s in the %s sentence "%s" is aligned with the ' \
              'following words in the %s sentence:' % (
                   source_position, 
                   source_lang,
                   aligned_sentences.get_word (AlignedSent.SOURCELANG, 
                                               source_position),
                   target_lang)
        
        # Iterate through alignments
        for alignment in alignments:
            print '\tword No.%s "%s" with confidence %s' % (
                    alignment[0],
                    aligned_sentences.get_word(AlignedSent.TARGETLANG,
                                                           alignment[0]),
                    alignment[1])


def use_case_2(translation,args):
    index = int(args[0])
    lang = args[1]
    aligned_sentences = translation[index]
    # Print Use Case Demo
    print "\nShow the %s sentence in sentence No.%s" % (lang, index)
    print aligned_sentences.whole_sentence (args[1])
            
def use_case_3(translation,args):
    index = int(args[0])
    aligned_sentences = translation[index]
    print "\nShow sentence No.%s and their word alignment mappings" % index
    print aligned_sentences.whole_sentence(AlignedSent.SOURCELANG)
    print aligned_sentences.whole_sentence(AlignedSent.TARGETLANG)
    for i,j in aligned_sentences.alignments():
        print '%s-%s' % (i,j),
    print

def use_case_4(translation,args):
    # search word, located where it occurs
    lang = args[0];
    query = args[1];
    print '\nShow all occurrences of the %s word "%s"' % (lang, query)
    keys = sorted(translation.keys());
    for key in keys:
        aligned_sentences = translation[key]
        for position in aligned_sentences.search_word(lang, query):
            print 'In sentence No. %s position %s' % (key, position);

def use_case_5(translation,args):
    # search word, statistical distribution of aligned words
    lang = args[0];
    if lang == AlignedSent.SOURCELANG:
        target_lang = AlignedSent.TARGETLANG
    else:
        target_lang = AlignedSent.SOURCELANG
    query = args[1]
    print '\nFind all %s words that are aligned with the %s word "%s" in the corpus' % \
            (target_lang,lang,query)
    keys = sorted(translation.keys())
    stats = defaultdict(int)
    total = 0
    for key in keys:
        aligned_sentences = translation[key]
        for position in aligned_sentences.search_word(lang, query):
            for position,confidence in aligned_sentences.nth_lookup(lang, position):
                al_word = aligned_sentences.get_word(target_lang,position)
                stats[al_word] += 1
                total += 1
                print 'In sentence No. %s position %s: "%s"' % \
                    (key, position,al_word)
    keys = sorted(stats.keys())
    for key in keys:
        print '"%s" x %s (%s)' % (key, stats[key], float(stats[key]) / total)